package com.anjubao.parking.municipal.user.component.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.anjubao.parking.municipal.common.utils.U;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信模板
 *
 * @author zhuchunfeng
 * @date 2019/6/24 9:23
 */
@Slf4j
public class AliSmsTemplate {

    private AliSmsProperties aliSmsProperties;
    private IAcsClient client;

    public AliSmsTemplate(AliSmsProperties aliSmsProperties) {
        log.info("ali sms properties ==> " + U.json(aliSmsProperties));
        this.aliSmsProperties = aliSmsProperties;
        DefaultProfile profile = DefaultProfile.getProfile(aliSmsProperties.getRegionId(), aliSmsProperties.getAccessKeyId(), aliSmsProperties.getSecret());
        client = new DefaultAcsClient(profile);
    }

    /**
     * 阿里云短信服务
     *
     * @param phone 手机
     * @param code  验证码
     */
    public void sendSms(String phone, String code) {

        log.info("发送短信 phone: {} code: {}",phone,code);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(aliSmsProperties.getDomain());
        request.setVersion("2017-05-25");
        request.setAction(AliSmsActionEnum.SEND_SMS.getCode());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "安居宝智慧停车");
        request.putQueryParameter("TemplateCode", "SMS_168585068");
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        request.putQueryParameter("TemplateParam", U.json(map));
        try {
//            CommonResponse response =
                    client.getCommonResponse(request);
//            log.info(U.json(response));
        } catch (ClientException e) {
            log.error("ClientException:{}",e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询发送记录
     * @param phone 手机
     * @param sendDate 时间
     * @param pageSize pageSize
     * @param currentPage currentPage
     * @return string
     */
    public String querySendDetails(String phone, LocalDate sendDate, Integer pageSize, Integer currentPage) {

        log.info("查询发送历史 phone: {} SendDate: {}", phone, sendDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(aliSmsProperties.getDomain());
        request.setVersion("2017-05-25");
        request.setAction(AliSmsActionEnum.QUERY_SEND_DETAILS.getCode());
        request.putQueryParameter("PhoneNumber", phone);
        request.putQueryParameter("SendDate", sendDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        request.putQueryParameter("PageSize", pageSize.toString());
        request.putQueryParameter("CurrentPage", currentPage.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(U.json(response));
            return response.getData();
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }





}
